home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 5685 / 5685.xpi / chrome / vodpod.jar / content / bookmarklet.js next >
Text File  |  2008-01-08  |  10KB  |  272 lines

  1. var vodpod = {
  2.   last_window : null,
  3.   root_window : null,
  4.   debug : false,
  5.   
  6.   share_video: function(document, target, root_window, target_server, debug_val) {
  7.     try {
  8.         this.root_window = root_window;
  9.         this.debug = debug_val;
  10.         var embeds = vodpod.bookmarklet_scrape(document);
  11.         if (this.debug) {
  12.           alert("Found these embeds: " + embeds);
  13.         }
  14.         var title = vodpod.scrape_title(document);
  15.         var desc = vodpod.scrape_description(document);
  16.         var tags = vodpod.scrape_tags(document);
  17.         
  18.         var my_form = document.createElement("form");
  19.         if (!target_server) {
  20.           target_server = "http://vodpod.com";
  21.         }
  22.         var target_path = "/widget/save_video_popup";
  23.         if (target == 'vodpod') {
  24.           target_path = "/widget/newptv";
  25.         }
  26.         my_form.action = target_server + target_path + "?target=" + escape(target) + '&bm=fe';
  27.         my_form.method = "POST";
  28.         my_form.target = "vodpod_share";
  29.  
  30.         vodpod.create_hidden_field(document, my_form, 'p', document.location.href);
  31.         vodpod.create_hidden_field(document, my_form, 'v', 4);
  32.         for( var i = 0; i < embeds.length; i++ ){
  33.             vodpod.create_hidden_field(document, my_form, 'embed[]', embeds[i]);
  34.         }
  35.         if (embeds.length > 0){
  36.             vodpod.create_hidden_field(document, my_form, 'em', embeds[0]);
  37.         }
  38.         vodpod.create_hidden_field(document, my_form, 't', document.title);
  39.         vodpod.create_hidden_field(document, my_form, 'd', desc);
  40.         vodpod.create_hidden_field(document, my_form, 'tags', tags);
  41.         
  42.         document.body.appendChild(my_form);
  43.         if (this.last_window) {
  44.           this.last_window.close();
  45.         }
  46.         var win_height = 550;
  47.         if (target == 'vodpod') {
  48.           win_height = 623;
  49.         }
  50.         this.last_window = root_window.open('','vodpod_share','toolbar=0,status=0,resizable=1,width=660,height=' + win_height);
  51.         my_form.submit();
  52.     } catch(e) {
  53.         alert("There was a problem opening the vodpod window [" + e + "]");
  54.     }
  55.   },
  56.   
  57.   create_hidden_field : function(document, the_form, name, value) {
  58.     var hiddenf = document.createElement("input");
  59.     hiddenf.type = 'hidden';
  60.     hiddenf.name = name;
  61.     hiddenf.value = value;
  62.     the_form.appendChild(hiddenf);
  63.   },
  64.   
  65.   scrape_title : function(document) {
  66.     return document.title;
  67.   },
  68.   
  69.   scrape_description : function(document) {
  70.     var elt = vodpod.getElementsByClassName(document, 'videoDescDiv expand-content'); 
  71.     if (elt[0]) {
  72.         return elt[0].innerHTML;
  73.     } else {
  74.         if ( (elt = document.getElementById('description')) ){ // google
  75.             return elt.innerHTML;
  76.         } else if ( (elt = vodpod.getElementsByClassName(document, 'description'))){ // dailymotion
  77.             if (elt[0]) {
  78.                 return elt[0].innerHTML;
  79.             }
  80.         }
  81.     }
  82.     return '';
  83.   },
  84.   
  85.   scrape_tags : function(document) {
  86.     var tags = '';
  87.     var elt = vodpod.getElementsByClassName(document, 'tagValues'); //youtube
  88.     if (elt.length > 0){
  89.         elt = elt[0];
  90.     } else {
  91.         elt = null;
  92.     }
  93.  
  94.     if (!elt) {
  95.         elt = document.getElementById('common_tags_short'); //google
  96.     }
  97.     if (!elt) {
  98.         elt = vodpod.getElementsByClassName(document, 'dm_widget_taglist');
  99.         if (elt.length > 0){
  100.             elt = elt[0];
  101.         }
  102.     }
  103.     
  104.     if (elt && elt.childNodes) {
  105.         for( var i = 0; i < elt.childNodes.length; i++ ){
  106.             var child = elt.childNodes[i];
  107.             if (child.tagName && child.tagName.toLowerCase() == 'a' ){
  108.                 tags = tags + child.innerHTML + ' ';
  109.             }
  110.         }
  111.     }
  112.     //alert("Returning tags: "+ tags);
  113.     return tags;
  114.   },
  115.   
  116.   bookmarklet_scrape: function(document) {
  117.       b=document.body.innerHTML;
  118.       ms = b.match(new RegExp('&'+'lt;embed.*?&'+'gt;', 'im'));
  119.       if(!ms){
  120.           ms=b.match(/\<embed.*?\>/im);
  121.       }
  122.       if (!ms) {
  123.         ms = new Array();
  124.       }
  125.       // Here's a nice trick. Some people (I'm looking at your Eyespot!) have the input field with the embed code,
  126.       // but set the field value using JS. So here we look for any input fields that have an embed/object code
  127.       // as their value.
  128.       inp_fields = document.getElementsByTagName("input");
  129.       for( var i = 0; i < inp_fields.length; i++ ){
  130.         val = inp_fields[i].value;
  131.         if (val.match(/\<embed.*/im)) {
  132.           ms.push(val);
  133.         }
  134.       }  
  135.       
  136.       // Now use the dom to find embeds
  137.       dom_ms = document.getElementsByTagName("embed");
  138.       // Now build a list of possible embeds. Our algorithm is to take 
  139.       // all embeds of approximately the right aspect ratio (4x3), then
  140.       // rank them by size
  141.       for (var i = 0; i < dom_ms.length; i++ ){
  142.         ms.push(dom_ms[i]);
  143.       }
  144.       if (ms.length == 0) {
  145.         // Look for object tags if no embeds
  146.         obj_tags = document.getElementsByTagName("object");
  147.         for( var j = 0; j < obj_tags.length; j++ ){
  148.           if (obj_tags[j].type == "application/x-shockwave-flash") {
  149.             var em_tag = '<embed src="' + obj_tags[j].data + '" type="application/x-shockwave-flash"' +
  150.                      ' height="' + obj_tags[j].height + '" width="' + obj_tags[j].width + '"';
  151.             var params = obj_tags[j].getElementsByTagName("param");
  152.             var flashvars = '';
  153.             for (var p = 0; p < params.length; p++) {
  154.               if (params[p].name == 'flashvars'){
  155.                 flashvars = params[p].value;
  156.                 break;
  157.               }
  158.             }
  159.             em_tag = em_tag + ' flashvars="' + flashvars + '">';
  160.             //alert("Found object tag, converted to embed: " + em_tag);
  161.             ms.push(em_tag);
  162.           }
  163.         }
  164.       }
  165.       //alert("MS: " + ms);
  166.       var candidates = new Array();
  167.       for( var i = 0; i < ms.length; i++ ){
  168.         var target = ms[i];
  169.         if (target.src) {
  170.             width = parseInt(target.width)
  171.             height = parseInt(target.height)
  172.             is_absolute = (target.src.match(/src=['"]\s*http/) != null);
  173.             if (width > 0 && height > 0){
  174.                 aspect = width / height
  175.                 if (aspect >= 1.0 && aspect <= 2.0) {
  176.                   // Convert embed to html
  177.                   ht = vodpod.element_to_html(target);
  178.                   candidates.push([width,is_absolute,ht]);
  179.                 }
  180.             }
  181.         } else {
  182.             // This is an embed found by src
  183.             try {
  184.               if (target.match(/</)){
  185.                 target = this.unescapeHTML(document,target);
  186.               }
  187.               width = vodpod.grep_dimension(target, /width=['"](\d+)/);
  188.               height = vodpod.grep_dimension(target, /height=['"](\d+)/);
  189.               is_absolute = (target.match(/src=['"]\s*http/) != null);
  190.               if (width > 0 && height > 0){
  191.                   aspect = width / height;
  192.                   if (aspect >= 1.0 && aspect <= 2.3) {
  193.                     candidates.push([width, is_absolute, target]);
  194.                   }
  195.               } else {
  196.                 //alert("discarding target: " + target);
  197.               }
  198.             } catch (e) {}
  199.         }
  200.       }
  201.       // Now sort the candidates by widest
  202.       candidates.sort( function(a,b) {
  203.         if (a[1] && !b[1]) {
  204.           return -1;
  205.         }
  206.         if (b[1] && !a[1]) {
  207.           return 1;
  208.         }
  209.         return b[0] - a[0];
  210.       } );
  211.       //alert(candidates);
  212.       result = new Array();
  213.       for( var i = 0; i < candidates.length; i++ ){
  214.         result.push(candidates[i][2]);
  215.       }
  216.       if (result.length == 0) {
  217.         // Didn't find anything, let's try to scrape frames...
  218.         var frames = document.getElementsByTagName("frame");
  219.         for( var f = 0; f < frames.length; f++ ){
  220.           try {
  221.             result = result.concat(this.bookmarklet_scrape(frames[f].contentDocument));
  222.           } catch (e) {} 
  223.         }        
  224.       }
  225.       return result;
  226.   },
  227.   grep_dimension : function(target, pattern) {
  228.     grep = target.match(pattern)
  229.     if (grep) {
  230.       return parseInt(grep[1]);
  231.     } else {
  232.       return 0;
  233.     }
  234.   },
  235.   element_to_html : function(element) {
  236.     var res = "<" + element.tagName + " ";
  237.     for( var i = 0; i < element.attributes.length; i++ ){
  238.         res = res + element.attributes[i].name + '="' + element.attributes[i].value + '" ';
  239.     }
  240.     res = res + "/>";
  241.     return res;
  242.   },
  243.   unescapeHTML : function(document, s) {
  244.     var div = document.createElement('div');
  245.     div.innerHTML = s;
  246.     var res = this.getTreeValues(div);
  247.     return res;
  248.   },
  249.   getTreeValues : function(elt) {
  250.      var res = '';
  251.     for( var i = 0; i < elt.childNodes.length; i++ ){
  252.       res = res + elt.childNodes[i].nodeValue;
  253.       res = res + this.getTreeValues(elt.childNodes[i]);
  254.     }
  255.     return res;
  256.   },
  257.   
  258.   getElementsByClassName:function(root, className) {
  259.     var children = root.getElementsByTagName('*');
  260.     var elements = new Array();
  261.     for( var i = 0; i < children.length; i++ ){
  262.       var child = children[i];
  263.       if (child.className.match(new RegExp("(^|\\s)" + className + "(\\s|$)"))){
  264.         elements.push(child);
  265.       }
  266.     }
  267.     return elements;
  268.   }
  269.   
  270. };
  271.  
  272.